package com.mju.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mju.dao.VipDao;
import com.mju.feign.clients.UserClient;
import com.mju.pojo.Vip;
import com.mju.utils.GetTheCurrentTimeUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class VipServiceImpl implements IVipService {
  @Autowired private VipDao vipDao;
  @Autowired private UserClient userClient;

  @Override
  public Boolean addNewVip(Vip vip,Integer day) {
    log.info("正在开通VIP...");
    // 根据uid查询用户是否曾是VIP
    Vip oldVip = vipDao.getByUid(vip.getUid());
    String currentTime = GetTheCurrentTimeUtils.getTheStrCurrentTime();

    if (Objects.isNull(oldVip)) {
      // 未曾开通过VIP，则添加新的VIP信息
      try {
        //计算VIP结束时间
        Date endTime = GetTheCurrentTimeUtils.getTheEndTime(currentTime, day);
        vip.setEndTime(endTime);
      } catch (ParseException e) {
        e.printStackTrace();
      }

      vip.setStatus(false);
      int result = vipDao.insert(vip);
      if (result!=1){
        log.error("开通VIP失败...");
      }
      //修改用户字段 1-有效
      userClient.updateIsVipAndVipName(vip.getUid(), 1, vip.getVipName());
      log.info("开通VIP成功...");
      return result==1;
    }else {
      // 开通过，则修改VIP信息，和计算结束时间 0-有效，1-过期
      // vip续期
      boolean result1 = vipDao.updateVIPStatus(vip.getUid(), false)>0;
      boolean result2 = this.vipRenewal(vip.getUid(), day, vip.getVipName());
      if (!result1 || !result2){
        log.error("续费VIP失败...");
      }
      //修改用户字段 1-有效
      userClient.updateIsVipAndVipName(vip.getUid(), 1, vip.getVipName());
      log.info("续费VIP成功...");
      return (result1&&result2);
    }
  }

  @Override
  public Vip getByUid(Integer uid) {
    log.info("查询VIP信息中...");
    return vipDao.getByUid(uid);
  }

  @Override
  public Boolean deleteVIP(Integer uid) {
    // 修改VIP信息 0-有效，1-过期
    log.info("删除VIP信息中...");
    boolean result = vipDao.updateVIPStatus(uid, true)>0;
    if (!result){
      log.error("删除VIP信息失败...");
      return null;
    }
    //修改用户字段 0-过期
    userClient.updateIsVip(uid,0);
    log.info("删除VIP信息成功...");
    return result;
  }

  //判断VIP是否过期 true-过期 false-未过期
  @Override
  public Boolean updateVIPStatus(Integer uid,Boolean status) {
    log.info("修改VIP状态中...");
    boolean result = vipDao.updateVIPStatus(uid, status)>0;
    if (!result){
      log.error("修改VIP状态失败...");
      return null;
    }
    //1-过期 0-有效
    userClient.updateIsVip(uid,status?0:1);
    log.info("修改VIP状态成功...");
    return result;
  }

  @Override
  public Vip queryVipByUid(Integer uid) {
    //校验VIP是否过期，如果过期则修改状态为过期
    log.info("校验VIP是否过期中...");
    boolean result = this.doesTheVipExpire(uid);
    if (!result){
      log.warn("VIP已过期...");
    }
    return vipDao.queryVipByUid(uid);
  }

  @Override
  public Boolean doesTheVipExpire(Integer uid) {
    Vip vip = getByUid(uid);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String vipTime = dateFormat.format(vip.getEndTime());

    String currentTime= GetTheCurrentTimeUtils.getTheStrCurrentTime();
    if(GetTheCurrentTimeUtils.compareTime(vipTime, currentTime)){
      //会员有效，校正状态 false-未过期
      this.updateVIPStatus(vip.getUid(),false);
      return true;
    }
    // 会员到期，更新VIP状态 true-已过期
    this.updateVIPStatus(vip.getUid(),true);
    return false;
  }


  @SneakyThrows
  @Override
  public Boolean vipRenewal(Integer uid, Integer day, String vipName) {
    log.info("续费VIP...");
    Vip vip = vipDao.getByUid(uid);
    Date time = this.doesTheVipExpire(uid) ? vip.getEndTime() : GetTheCurrentTimeUtils.getTheCurrentTime();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date endTime = GetTheCurrentTimeUtils.getTheEndTime(dateFormat.format(time), day);
    vip.setEndTime(endTime);
    vip.setVipName(vipName);
    int result = vipDao.updateById(vip);
    if (result==0){
      log.error("VIP续费失败...");
      return false;
    }
    //1-过期 0-有效
    userClient.updateIsVipAndVipName(uid, 1, vipName);
    //续费成功
    log.info("VIP续费成功...");
    return result>0;
  }

  @Override
  public IPage<Vip> queryAllVipInfo(Integer currentPage, Integer pageSize) {
    IPage page = new Page<>(currentPage, pageSize);
    vipDao.selectPage(page,null);
    return page;
  }

  @Override
  public IPage<Vip> conQueryAllVipInfo(Integer currentPage, Integer pageSize, Vip vip) {
    IPage page = new Page<>(currentPage, pageSize);
    LambdaQueryWrapper<Vip> lwq=new LambdaQueryWrapper<>();
    lwq.eq(!Objects.isNull(vip.getUid()), Vip::getUid, vip.getUid());
    lwq.eq(!Objects.isNull(vip.getStatus()),Vip::getStatus,vip.getStatus());
    vipDao.selectPage(page,lwq);
    return page;
  }
}
